package com.konne.nightmare.FastPublicOpinion.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/1/24  Time:下午 8:46.
 * description: 添加老师弹窗 .
 */
public class AddTeacherDialog extends Dialog {
    private AddTeacherListener addTeacherListener;
    public AddTeacherDialog(@NonNull Context context) {
        super(context);
    }

    public AddTeacherDialog setAddTeacherListener(AddTeacherListener addTeacherListener) {
        this.addTeacherListener = addTeacherListener;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_teacher);
        EditText et_txt = findViewById(R.id.et_txt);
        et_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_GO) {
                    if(detectionTxt() && addTeacherListener!=null){
                        addTeacherListener.addTeacher(et_txt.getText().toString());
                        dismiss();
                    }
                    return true;
                }
                return false;
            }
        });
        findViewById(R.id.stv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        findViewById(R.id.stv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(detectionTxt() && addTeacherListener!=null){
                    addTeacherListener.addTeacher(et_txt.getText().toString());
                    dismiss();
                }
            }
        });
    }

    private boolean detectionTxt() {
        EditText et_txt = findViewById(R.id.et_txt);
        boolean isCanInput = et_txt.getText().length() > 0;
        findViewById(R.id.stv_input_error).setVisibility(isCanInput ? View.INVISIBLE : View.VISIBLE);
        return isCanInput;
    }
    public interface AddTeacherListener{
        void addTeacher(String id);
    }
}
